package hello;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import video.MIDPPixelSource;


public class TrackObject extends MIDlet {

	VideoCanvas myCanvas;

	MIDPPixelSource ps;

	int[] target = { 255, 155, 55 };

	public void startApp() throws MIDletStateChangeException {

		myCanvas = new VideoCanvas();
		ps = new MIDPPixelSource(160, 120, null, myCanvas);

		Display display = Display.getDisplay(this);
		display.setCurrent(myCanvas);
		// ps = new MIDPPixelSource(160, 120,"http://itp.nyu.edu/~dbo3/cellphone/testvid/h.MPG",myCanvas);
		// ps = new MIDPPixelSource(160, 120,"http://java.sun.com/products/java-media/mma/media/test-mpeg.mpg",myCanvas);
		// ps = new MIDPPixelSource(160, 120,true);

		 ps.addVideoListener(myCanvas, 15);

	}

	public void pauseApp() {
	}

	public void destroyApp(boolean arg0) throws MIDletStateChangeException {
		System.out.println("destroy");
	}

	public class VideoCanvas extends Canvas {

		int x;

		int y;

		Image myImage;

		public void keyPressed(int keyCode) {

			System.out.println("Keycode" + keyCode);
			if (keyCode == Canvas.KEY_NUM5) {
				// ps.getPixelArray(null);
				ps.grabFrame();
				ps.getPixelArray(null);
				int[] rgb = ps.getPixel( ps.getImageWidth() / 2,  ps.getImageHeight() / 2);
				target[0] = rgb[1];
				target[1] = rgb[2];
				target[2] = rgb[3];
				System.out.println("new target" + rgb[1] + " " + rgb[2] + " " + rgb[3]);
			} else if (keyCode == Canvas.KEY_NUM0) {
				// thanks to stefan

				try {
					destroyApp(true);
				} catch (MIDletStateChangeException e) {
					e.printStackTrace();
				}

				notifyDestroyed();
			}

		}

		public void paint(Graphics g) {
			
			if (ps.requiresManualPainting()){
				ps.grabFrame();
				Image snapshot = ps.getImage();
				g.drawImage(snapshot, x, y, 0);
			}

			int bestX = 100;
			int bestY = 100;
			int closest = 2000;
			ps.grabFrame();
			ps.getPixelArray(null);

			for (int row = 0; row < ps.getImageHeight(); row++) {
				for (int col = 0; col < ps.getImageWidth(); col++) {
					int[] rgb = ps.getPixel(col, row);
					int rdiff = rgb[1] - target[0];
					int gdiff = rgb[2] - target[1];
					int bdiff = rgb[3] - target[2];
				
					int diff = (int) Math.sqrt(rdiff * rdiff + gdiff * gdiff + bdiff * bdiff);
					if (diff < closest) {
						bestX = col;
						bestY = row;
						closest = diff;
					}
				}
			}
			g.setColor(255, 255, 255);
			g.fillRect(0, ps.getImageHeight(), ps.getImageWidth()+5, ps.getImageHeight()+5);

			g.setColor(255, 0, 0);
			g.fillRect(bestX - 3, bestY - 3 + ps.getImageHeight(), 6, 6);
			// draw the sampling point
			g.setGrayScale(255);
			g.drawRect(ps.getImageWidth() / 2 - 5, ps.getImageWidth() / 2 - 5, 10, 10);
			g.drawString("Pick Color Press 5", ps.getImageWidth() / 4 - 5, ps.getImageHeight() / 2 - 5 , 0);

		}

	}

}
